package com.tyut.farmerservice.inteceptor;

import com.tyut.farmerservice.result.StatusCode;
import com.tyut.farmerservice.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@ResponseBody
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler{
    //主键重复
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public Result<String> sqlIntegrityConstraintViolationException() {
        return new Result<String>(false, StatusCode.ERROR, "账号已被注册，请重新输入", "账号已被注册");
    }
    //    请求方式不正确
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Result<String> httpRequestMethodNotSupportedException(){
        System.out.println("请求方式异常，httpRequestMethodNotSupportedException");
        return new Result<String>(false, StatusCode.ERROR,"请求方式不正确","请求方式不正确，HttpRequestMethodNotSupportedException");
    }
    //    类转换异常
    @ExceptionHandler(ClassCastException.class)
    public Result<String> classCastException(){
        System.out.println("类转换异常，这个项目多半发生在请求头的token问题");
        return new Result<String>(false, StatusCode.ERROR,"请您先登录","请您先登录");
    }

}
